package nckd.wnqjt.tmc.tda.plugin.form;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import kd.bos.algo.DataSet;
import kd.bos.entity.report.AbstractReportListDataPlugin;
import kd.bos.entity.report.ReportQueryParam;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;

/**
 * @author husheng
 * @date 2024-11-04 20:08
 * @description
 */
public class GuaranteetypeListDataPlugin extends AbstractReportListDataPlugin {
    @Override
    public DataSet query(ReportQueryParam reportQueryParam, Object o) {
        String guaranteecontractSql = "id,org.id orgId,org.name nckd_orgname,org.number orgNumber,bizdate nckd_bizdate," +
                "guaranteeway nckd_guaranteeway,billno nckd_billno,guaranteeno nckd_guaranteeno,currency nckd_currencys," +
                "entry_guaranteeorg.id,entry_guaranteeorg.a_gamount nckd_amount,entry_guaranteeorg.a_gratio";
        QFilter filter1 = new QFilter("billstatus", QCP.equals, "C");
        DataSet gmGuaranteecontract = QueryServiceHelper.queryDataSet(this.getClass().getName(), "gm_guaranteecontract", guaranteecontractSql, filter1.toArray(), null);

        // 担保债务-担保中
        QFilter gQF = new QFilter("billstatus", QCP.equals, "C")
                .and("gdebtstartdate", QCP.less_equals, this.getCurrentDate())
                .and("gratio", QCP.not_equals, BigDecimal.ZERO)
                .and("gdebtbalance", QCP.not_equals, BigDecimal.ZERO)
                .and(new QFilter("gsrcbilltype", QCP.not_equals, "cdm_payablebill")
                        .or("gdebtenddate", QCP.is_null, null)
                        .or("gdebtenddate", QCP.large_than, this.getCurrentDate()));
        DataSet gcUseBills = QueryServiceHelper.queryDataSet(this.getClass().getName(), "gm_guaranteeuse", "id,gcontract.id gcontractId,gdebtbalance", gQF.toArray(), null);
        DataSet finish = gcUseBills.groupBy(new String[]{"gcontractId"}).sum("gdebtbalance").finish();

        DataSet guaranteecontract = gmGuaranteecontract.leftJoin(finish).on("id", "gcontractId").select(gmGuaranteecontract.getRowMeta().getFieldNames(), new String[]{"gdebtbalance"}).finish();
        DataSet dataSet = guaranteecontract.select(new String[]{"nckd_orgname", "nckd_bizdate", "nckd_guaranteeway", "nckd_billno", "nckd_guaranteeno", "nckd_currencys", "entry_guaranteeorg.id", "nckd_amount", "(entry_guaranteeorg.a_gratio * gdebtbalance / 100) nckd_gdebtbalance"});

        //筛选出 筛选器传入的ids
        Map<String, Object> customParam = reportQueryParam.getCustomParam();
        List<String> list = (ArrayList<String>) customParam.get("id");
        List<Long> longList = list.stream().map(e -> Long.valueOf(e)).collect(Collectors.toList());
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < longList.size(); i++) {
            Long id = longList.get(i);
            if (longList.size() - 1 == i) {
                sb.append(id);
            } else {
                sb.append(id).append(",");
            }
        }
        dataSet = dataSet.filter("entry_guaranteeorg.id in (" + sb + ")");
        return dataSet;
    }

    private Date getCurrentDate() {
        Calendar cal = new GregorianCalendar();
//        cal.set(11, 0);
//        cal.set(12, 0);
//        cal.set(13, 0);
//        cal.set(14, 0);
        return cal.getTime();
    }
}
